<?php

namespace frontend\controllers;

use Yii;
use common\models\meeting\Meeting;
use common\models\meeting\MeetingSearch;
use common\models\meeting\MeetingSign;
use common\models\meeting\Meetingenroll;
use common\models\AdminLog\AdminLog;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\data\Pagination;
use yii\base\Object;

/**
 * ArticlesController implements the CRUD actions for Articles model.
 */
class MeetingenrollController extends Controller
{
    public $layout = 'ucenter';
    /**
     * 报名
     * $id 会议id
     */
	public function actions()
    {
        return [
            'upload'=>[
                'class' => 'common\widgets\file_upload\UploadAction',     //这里扩展地址别写错
                'config' => [
                    'imagePathFormat' => "/image/{yyyy}{mm}{dd}/{time}{rand:6}",
                ]
            ]
        ];
    }
    public function actionCreate($id)
    {
		$res = Meeting::find()->where(['id' => $id])->one();
		$model = new Meetingenroll();
 		if($res->enroll == 1){
			if(Yii::$app->user->id){
				if ($model->load(Yii::$app->request->post())) {
					$model->meeting_id = $id;
					$model->created_at = time();
					$model->member_id = Yii::$app->user->id;
					if($model->save()){
						//return $this->redirect(['view', 'id' => $model->id]);
					}
				}else{
					return $this->render('create',[
						'model' => $model
					]);
				}
			}else{
				return $this->redirect(['/auth/login']);
			}
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }
    
	
 	public function actionView($id)
    {
		$model = $this->findModel($id);
		Adminlog::addlog(yii::$app->user->id, "查看在线留言 ".$model->title);
        return $this->render('view', [
            'model' => $model,
        ]);
    }

    /**
     * Lists all Articles models.
     * @return mixed
     */
    public function actionList()
    {
		$id = Yii::$app->user->id;
		$data = Meetingenroll::find()->where(['member_id' => $id])->asArray()->all();
		if(empty($data)){
			$model = NULL;
		}else{
			foreach($data as $k=>$val){
				//var_dump($val['meeting_id']);die;
				$dataa = Meeting::find()->where(['id' => $val['meeting_id']])->asArray()->one();
				//var_dump($aa);die;
				//var_dump($dataa);die;
				if($dataa){
				$aa = MeetingSign::find()->where(['meeting_id' => $val['meeting_id'], 'member_id' => $val['member_id']])->one();
				$dataa['sign'] = !empty($aa) ? 1 : 0;
					$model[] = $dataa;
					//$model[] = array('sign' => 1);
					//var_dump($model);die;
				}
			}
		}
		//$model['sign'] = array();
		//$model = array_merge($model,$model['sign']);
		return $this->render('list',[
			'model' => $model,
		]);
    }
	
/* 	public function actionList(){

		$cid = Yii::$app->request->get()["cid"];
		$artice_model = Articles::find()->where(['category_id' => $cid]);
		$pages = new Pagination(['totalCount'=>$artice_model->count(), 'pageSize'=>1]);	//分页
		//动态读取内容
		$datas = $artice_model->offset($pages->offset)
		->limit($pages->limit)
		->orderby('id DESC')
		->all();
		return $this->render('list',['list' => $datas,'pages' => $pages]);
	}
 */    
	
    /**
     * Finds the Articles model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Articles the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Meeting::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
